package com.example.apple.paopo.fragment.world;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.OrientationHelper;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.apple.paopo.R;
import com.example.apple.paopo.adapter.mRecommendedAdapter;
import com.example.apple.paopo.base.BaseFragment;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.vondear.rxtools.view.RxToast;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.ArrayList;

/**
 * Created by apple on 17/12/10.
 */
//推荐
    @ContentView(R.layout.fragment_recommended)
public class RecommendedFragment extends BaseFragment{

    @ViewInject(R.id.recommended_refreshLayout)
    RefreshLayout refreshLayout;
    @ViewInject(R.id.recommended_recyclerview)
    RecyclerView recyclerView;

    ArrayList<String> mdatas;

    com.example.apple.paopo.adapter.mRecommendedAdapter mRecommendedAdapter;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view= x.view().inject(this,inflater,container);
        initView(view);
        return view;
    }
    private void initView(View view) {
        initDatas();
        setUpRecyclerView();
        setUpRefreshAndLoadmore();
        refreshLayout.autoRefresh();

    }

    private void initDatas() {
        mdatas= new ArrayList<>();
        mdatas.add("sdadsa");
        mdatas.add("sdadsa");

    }

    private void setUpRecyclerView() {
        mRecommendedAdapter=new mRecommendedAdapter(getContext(),mdatas);
        LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
//设置布局管理器
        recyclerView.setLayoutManager(layoutManager);
//设置为垂直布局，这也是默认的
        layoutManager.setOrientation(OrientationHelper. VERTICAL);
//设置Adapter
        recyclerView.setAdapter(mRecommendedAdapter);
//        //设置分隔线
//        recyclerView.addItemDecoration(new DividerGridItemDecoration(getContext()));
//设置增加或删除条目的动画
        recyclerView.setItemAnimator(new DefaultItemAnimator());
    }


    private void setUpRefreshAndLoadmore() {
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                RxToast.success("我要开始了");
                mdatas.add("sdadsa");
                mdatas.add("sdadsa");
                refreshlayout.finishRefresh(2000/*,false*/);//传入false表示刷新失败
                mRecommendedAdapter.notifyDataSetChanged();

            }
        });
        refreshLayout.setOnLoadmoreListener(new OnLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                RxToast.success("我要开始了");
                mdatas.add("sdadsa");
                mdatas.add("sdadsa");
                refreshlayout.finishLoadmore(2000/*,false*/);//传入false表示加载失败
                mRecommendedAdapter.notifyDataSetChanged();
            }
        });
    }
}
